﻿using AutoMapper;
using MediatR;
using S.Breeding.Api.Application.Command.LambWeaningCommand;
using S.Breeding.Domain;
using S.Breeding.Infrastructure;

namespace S.Breeding.Api.Application.CommandHandlers.LambWeaningCommandHandlers
{
    public class LambWeaningCreateCommandHandlers : IRequestHandler<LambWeaningCreateCommand, int>
    {
        private readonly IRepository<LambWeaning> repository;
        private readonly IMapper mapper;
        public LambWeaningCreateCommandHandlers(IRepository<LambWeaning> repository, IMapper mapper)
        {
            this.repository = repository;
            this.mapper = mapper;
        }

        public async Task<int> Handle(LambWeaningCreateCommand request, CancellationToken cancellationToken)
        {
            var result = repository.AllAsync().Result.Where(x => x.LambOverbit == request.LambOverbit && x.WeaningId != request.WeaningId);
            if (result.Count() != 0)
            {
                return -1;
            }
            request.CreateBy = "阿衰";
            request.CreateDate = DateTime.Now;
            return await repository.CreateAsync(mapper.Map<LambWeaning>(request));
        }
    }
}
